<?php

namespace app\api\controller;

use app\essayhandle\controller\Addessay;
use think\Db;
use think\Exception;
use WebGeeker\Validation\Validation;


//视频相关

class Video extends NewsBase
{

    //需要获取的视频数据的字段
    private static $video_field = 'author_id,remark_numbers,favour_numbers,title,video_url,read_numbers,item_count';

    //视频卡片页面
    public function getVideoCardOneList()
    {
        try {

            // 验证（如果验证不通过，会抛出异常)
            Validation::validate($_GET, [
                "item_id" => "Required|Str|StrLenGe:1",
            ]);

            //判断当前是不是视频
            $item_id = $_GET['item_id'];

            $item_obj = $this->mongo_obj->name('item');

            $item_result = $item_obj->where(['id' => $item_id, 'classify_type' => 2])->field(self::$video_field)->find();

            if (!$item_result) {
                throw new Exception('视频数据不存在');
            }

            $list[] = $item_result;
            $this->handle($list,1);
        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //视频数据处理,$type 表示 数据处理的分类,1表示单条数据,2表示多条数据
    private function handle($list,$type)
    {
        $user_obj = Db::name('user');
        foreach ($list as $key => $value) {

            $author_id = $value['author_id'];

            $user_info = $user_obj->where(['user_unique' => $author_id])
                ->field('nick_name,head_img,intro,is_member,is_approve,member_end_time')->find();

            $value['nick_name'] = $user_info['nick_name'];

            $value['head_img'] = $user_info['head_img'];

            $value['intro'] = $user_info['intro'];

            $value['is_approve'] = $user_info['is_approve'];

            if ($user_info['is_member'] == 0) {
                $value['is_member'] = 0;
            } else {
                if ($user_info['member_end_time'] < time()) {
                    $value['is_member'] = 0;
                } else {
                    $value['is_member'] = 1;
                }
            }

            //判断是不是关注了这个用户
            switch (self::$user_type) {
                case 'login':

                    //判断用户是不是已经关注了作者
                    $attention_result = $this->mongo_obj
                        ->name('user_attention_record')
                        ->where(['follower_user_id' => $this->user_id, 'by_follower_user_unique' => $author_id, 'status' => 1])
                        ->find();

                    if ($attention_result) {
                        $value['attention'] = 1;
                    } else {
                        $value['attention'] = 0;
                    }

                    //获取用户是不是收藏了这个视频
                    $collect_result = $this->mongo_obj
                        ->name('item_collect_record')
                        ->where(['collect_user_id' => $this->user_id, 'item_id' => $value['id'], 'collect_status' => 1])
                        ->find();

                    if ($collect_result) {
                        $value['collect_status'] = 1;
                    } else {
                        $value['collect_status'] = 0;
                    }
                    break;

                case 'nog_login':
                    $value['attention'] = 0;
                    $value['collect_status'] = 0;
                    break;
            }

            //获取用户的点赞状态
            $favour_status_info = $this->mongo_obj->name('item_favour_record')
                ->where(['user_id' => $this->user_id, 'item_id' => $value['id']])
                ->find();

            if ($favour_status_info) {
                $value['favour_status'] = $favour_status_info['status'];
            } else {
                $value['favour_status'] = 0;
            }

            $list[$key] = $value;
        }

        $data = [];

        switch ($type){
            case 1:
                $data = $list[0];
                break;
            case 2:
                $data['list'] = $list;
                break;
        }

        successBack($data);
    }

    //获取卡片页智能推荐的数据
    public function getVideoCardAirecList()
    {
        try {
            // 验证（如果验证不通过，会抛出异常)
            Validation::validate($_GET, [
                "item_id" => "Required|Str|StrLenGe:1",
            ]);

            //判断当前是不是视频
            $item_id = trim($_GET['item_id']);

            $item_obj = $this->mongo_obj->name('item');

            $item_result = $item_obj->where(['id' => $item_id, 'classify_type' => 2])->field('classify_scene_id')->find();

            if (!$item_result) {
                throw new Exception('视频数据不存在');
            }

            //获取智能推荐的数据
            //场景id
            $scene_id = $item_result['classify_scene_id'];

            $item_id_arr = getCommonAirecResult($scene_id, self::$user_type, $this->user_unique, $this->imei);


            $list = $item_obj->where('id', 'in', $item_id_arr)->field(self::$video_field)->select();

            $this->handle($list,2);
        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //获取视频的关注列表
    public function getVideoAttentionList(){
        try {
            Validation::validate($_GET, [
                "item_count" => "Required|IntGe:0"
            ]);

            switch (self::$user_type){
                case 'login':

                    $item_count = (int)$_GET['item_count'];

                    $user_feed_obj = Db::connect('db_mongo')->name('user_feed');

                    $user_feed_condition = [];

                    $user_feed_condition['user_id'] = ['=',$this->user_id];

                    $user_feed_condition['classify_type'] = 2;

                    if($item_count != 0){
                        //不是首页
                        $user_feed_condition['item_count'] = ['<',$item_count];
                    }


                    //获取当前用户feed流中的数据
                    $feed_list = $user_feed_obj->where($user_feed_condition)->order(['item_count' => -1])->limit(5)->select();

                    if($feed_list){

                        $item_id_arr = [];

                        foreach ($feed_list as $value){
                            $item_id_arr[] = $value['item_id'];
                        }


                        $list = $this->mongo_obj->name('item')->where('id', 'in', $item_id_arr)->field(self::$video_field)->order(['item_count' => -1])->select();


                    }else{
                        $list = [];
                    }
                    break;
                case 'not_login':
                    $list = [];
                    break;
            }

            $this->handle($list,2);
        }catch (\Exception $exception){
            errorBack($exception);
        }
    }


    //获取除关注外的视频列表
    public function getVideoList()
    {
        try {
            Validation::validate($_GET, [
                "navigation_name" => "Required|Str|StrLenGe:1",
            ]);

            $navigation_name = $_GET['navigation_name'];

            if ($navigation_name == '关注') {
                throw new Exception('navigation_name参数值错误,不能是关注');
            }

            //判断当前导航存在不存在
            $result = $this->mongo_obj->name('item_navigation')->where(['navigation_name' => $navigation_name, 'navigation_type' => 2])->field('scene_id')->find();

            if (!$result) {
                throw new Exception('导航不存在');
            }

            //场景的id
            $scene_id = $result['scene_id'];

            $item_id_arr = getCommonAirecResult($scene_id, self::$user_type, $this->user_unique, $this->imei);

            $item_obj = $this->mongo_obj->name('item');

            $list = $item_obj->where('id', 'in', $item_id_arr)->field(self::$video_field)->select();

            $this->handle($list,2);
        } catch (\Exception $exception) {
            errorBack($exception);
        }

    }

    //视频举报
    public function videoReport()
    {
        try {
            Validation::validate($_GET, [
                "item_id" => "Required|Str|StrLenGe:1",
                "report_content" => "Required|Str|StrLenGe:1"
            ]);

            //视频的id
            $item_id = $_GET['item_id'];

            //举报的内容
            $report_content = trim($_GET['report_content']);

            //判断item_id是否存在
            $item_obj = $this->mongo_obj->name('item');
            $item_obj->where(['id' => $item_id])->findOrFail();


            //插入的数据
            $addData = [];

            $addData['item_id'] = $item_id;

            $addData['report_content'] = $report_content;

            $addData['report_time'] = time();

            $addData['report_user_id'] = $this->user_id;

            if (isset($_GET['feedback'])) {
                //举报的内容
                $feedback = trim($_GET['feedback']);

                $addData['feedback'] = $feedback;
            }

            $result = $this->mongo_obj->name('video_report_record')->insertGetId($addData);

            if ($result) {
                successBack([], '举报成功');
            } else {
                successBack([], '举报失败');
            }

        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    //视频详情页获取接口
    public function getVideoDetails()
    {
        try {
            Validation::validate($_GET, [
                "item_id" => "Required|Str|StrLenGe:1"
            ]);

            $item_id = trim($_GET['item_id']);

            $item_obj = $this->mongo_obj->name('item');

            $item_result = $item_obj->where(['id' => $item_id, 'classify_type' => 2])->field(self::$video_field . ',content,pub_time')->find();

            $item_result['pub_time'] = time_tran($item_result['pub_time'], time());

            $list[] = $item_result;

            $this->handle($list,1);
        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }
}